package com.link.base.ext.autocfg.service;

import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.base.ext.autocfg.dao.mybatis.mapper.AutoCfgItemSubMapper;
import com.link.base.ext.autocfg.model.AutoCfgItemSub;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class AutoCfgItemSubServiceImpl extends BasicServiceImpl<AutoCfgItemSub> implements AutoCfgItemSubService {

    @Resource
    private AutoCfgItemSubMapper autoCfgItemSubMapper;

    @Override
    public BasicMapper<AutoCfgItemSub> getBasicMapper() {
        return autoCfgItemSubMapper;
    }

    @Transactional(rollbackFor = BasicServiceException.class)
    @Override
    public int deleteByParentId(AutoCfgItemSub record) {
        // TODO Auto-generated method stub
        return autoCfgItemSubMapper.deleteByParentId(record);
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void update(AutoCfgItemSub record) throws Exception {
        record.setBasicStandardId(null);//有更新表示该配置已被重写，与母版已无关系
        super.update(record);
        autoCfgItemSubMapper.updateLinkRecordByBasicId(record);
    }

    @Transactional(rollbackFor = BasicServiceException.class)
    @Override
    public void updateLinkItemSub(AutoCfgItemSub record) throws Exception {

    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void deleteById(AutoCfgItemSub record) throws Exception {
        super.deleteById(record);
        autoCfgItemSubMapper.deleteLinkRecordByBasicId(record);
    }

    @Override
    public void upsertAndDel(List<AutoCfgItemSub> list) throws Exception {
        // TODO Auto-generated method stub
        for (AutoCfgItemSub record : list) {
            String op = record.getRow_status();
            switch (op) {
                case "UPDATE":
                    update(record);
                    break;

                case "NEW":
                    insert(record);
                    break;

                case "DELETE":
                    deleteById(record);
                    break;

                default:
                    break;
            }
        }
    }
}
